import {AfterViewInit, Component, OnInit, ViewChild} from '@angular/core';
import {MessageService, ModalComponent, PaginationService, ProgressBarService, Table} from '../../../../../ng-tools';
import {TicketService} from '../../../../services/api/ticket.service';
import {NavigationExtras, Router} from '@angular/router';
import {HttpParams} from '@angular/common/http';
import {RobotGroupFormatterService} from '../../../../services/formatter/robot-group-formatter.service';
import {RobotProduceComponent} from '../robot-produce/robot-produce.component';
import {RobotGroupItemConfigComponent} from '../robot-group-item-config/robot-group-item-config.component';

@Component({
  selector: 'robot-group',
  templateUrl: './robot-group.component.html',
  styleUrls: ['./robot-group.component.scss']
})
export class RobotGroupComponent implements OnInit, AfterViewInit {
  @ViewChild('modal') private modal: ModalComponent;
  breadcrumb = [
    {name: '机器人组', cssIcon: 'fa fa-fw fa-android'}
  ];
  page = 1;
  configRoobotGroup;
  editRoobotGroup;
  pagenation = new PaginationService(null, null);
  chargeTable = new Table({
    headers: this.robotGroupFormatter.formatter,
    buttons: [
      {
        btnClass: 'btn btn-danger btn-sm mr-1 px-1 py-0',
        text: '基本配置',
        isShow: true,
        click: (e) => {
          this.configRoobotGroup = e;
          this.showRobotGroupEdit();
        }
      },
      {
        btnClass: 'btn btn-danger btn-sm mr-1 px-1 py-0',
        text: '成员编辑',
        isShow: true,
        click: (e) => {
          this.editRoobotGroup = e;
          this.showEditRobotGroup();
        }
      }
    ]
  });

  constructor(private message: MessageService,
              private ticketService: TicketService,
              public progress: ProgressBarService,
              private router: Router,
              private robotGroupFormatter: RobotGroupFormatterService,
  ) {
  }

  ngOnInit() {
    this.getInfo(1);

  }

  showEditRobotGroup() {
    const navigationExtras: NavigationExtras = {
      queryParams: { 'groupID': this.editRoobotGroup.id },
    };
    this.router.navigate(['main/oplatform/robot-group-list'], navigationExtras).catch();
  }
  showRobotGroupEdit() {
    this.modal.show({
      title: '编辑机器人组',
      cssIcon: 'fa fa-fw fa-plus-circle',
      dynamicComponent: {
        component: RobotGroupItemConfigComponent,
        data: this.configRoobotGroup,
        events: {
          success: () => {
            this.modal.hide();
            this.getInfo(this.page);
          }
        },
      },
      data: null
    });
  }
  addRobotGroup() {
    this.modal.show({
      title: '添加机器人组',
      cssIcon: 'fa fa-fw fa-plus-circle',
      dynamicComponent: {
        component: RobotGroupItemConfigComponent,
        data: null,
        events: {
          success: () => {
            this.modal.hide();
            this.getInfo(this.page);
          }
        },
      },
      data: null
    });
  }
  getInfo(page) {
    this.pagenation.simpleFormatter = this.chargeTable.options.headers;
    this.ticketService.get(this.ticketService.urls.getRobotsGroup, {
      observe: 'response',
      params: new HttpParams()
          .append('web_token', localStorage.getItem('token'))
          .append('pageNum', page + '')
          .append('pageSize', '10')}).subscribe((resp) => {
      if (resp.body.success) {
        const result = resp.body.returnValue.page;
        this.pagenation.page.results = result.list;
        this.pagenation.page.totalPage = result.pages;
        this.pagenation.page.pageNumber = result.pageNum;
        this.pagenation.formatData();
        this.message.sendMessage('page load progress', this);
      } else {
        this.router.navigateByUrl('/login').catch();
      }
    });
  }
  firstPage() {
    this.page = 1;
    this.getInfo(1);
  }
  previousPage() {
    if (this.page > 1) {
      this.page = this.page - 1;
    } else {
      this.page = 1;
    }
    this.getInfo(this.page);
  }
  nextPage() {
    if (this.page < this.pagenation.page.totalPage) {
      this.page = this.page + 1;
    } else {
      this.page = this.pagenation.page.totalPage;
    }
    this.getInfo(this.page);
  }
  lastPage() {
    this.page = this.pagenation.page.totalPage;
    this.getInfo(this.pagenation.page.totalPage);
  }
  ngAfterViewInit(): void {
    this.message.sendMessage('breadcrumb changed', this, this.breadcrumb);
    this.message.sendMessage('page load progress', this, '300ms');
  }

}
